package com.kaishengit.service;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.kaishengit.dao.MailDao;
import com.kaishengit.pojo.Company;
import com.kaishengit.pojo.Contacts;
import com.kaishengit.pojo.Mail;
import com.kaishengit.util.DateUtil;

@Transactional
public class MailService {

	private MailDao mailDao;
	
	public void savemail(String[] mail, String[] mailtype, Contacts contacts) {
		mailDao.delAll(findByObject(contacts));
		if(mail != null && mailtype != null) {
			for(int i = 0; i < mail.length; i ++) {
				for(int j = 0; j < mailtype.length; j ++) {
					if(i == j) {
						Mail email = new Mail();
						email.setMail(mail[i]);
						email.setType(mailtype[j]);
						email.setCreatetime(DateUtil.getNow());
						email.setContacts(contacts);
						mailDao.save(email);
					}
				}
			}
			
		}
	}

	
	
	public MailDao getMailDao() {
		return mailDao;
	}

	public void setMailDao(MailDao mailDao) {
		this.mailDao = mailDao;
	}



	public void savemail(String[] mail, String[] mailtype, Company company) {
		if(mail != null && mailtype != null) {
			for(int i = 0; i < mail.length; i ++) {
				for(int j = 0; j < mailtype.length; j ++) {
					if(i == j) {
						Mail email = new Mail();
						email.setMail(mail[i]);
						email.setType(mailtype[j]);
						email.setCreatetime(DateUtil.getNow());
						email.setCompany(company);
						mailDao.save(email);
					}
				}
			}
			
		}
		
	}



	public List<Mail> findByObject(Contacts contacts) {
			String hql = "select h from Mail as h where h.contacts.id = ?";
			return mailDao.find(hql, contacts.getId());
		}



	public void delmail(Contacts contacts) {
		mailDao.delAll(findByObject(contacts));
	}
	}

	
	
	
